package org.longxin.domains;

// Generated 2015-1-17 19:49:59 by Hibernate Tools 3.4.0.CR1

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 * Feature generated by hbm2java
 */
@Entity
@Table(name = "feature", catalog = "longxin")
public class Feature implements java.io.Serializable, Cloneable
{
    private static final long serialVersionUID = 4452069230447771945L;

    private Integer id;

    private Product product;

    private String featureName;

    private Boolean template;

    private String description;

    private String iconName;

    private String functionName;

    private Set<Module> modules = new HashSet<Module>(0);

    private Set<FunctionModule> functionModules = new HashSet<FunctionModule>(0);

    private Boolean searched;
    
    private List<Module> tempModules = new ArrayList<Module>();

    public Feature()
    {
    }

    public Feature(Product product, String featureName)
    {
        this.product = product;
        this.featureName = featureName;
    }

    public Feature(Product product, String featureName, Boolean template, Set<Module> modules)
    {
        this.product = product;
        this.featureName = featureName;
        this.template = template;
        this.modules = modules;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId()
    {
        return this.id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PRODUCT", nullable = false)
    public Product getProduct()
    {
        return this.product;
    }

    public void setProduct(Product product)
    {
        this.product = product;
    }

    @Column(name = "FEATURE_NAME", nullable = false, length = 128)
    public String getFeatureName()
    {
        return this.featureName;
    }

    public void setFeatureName(String featureName)
    {
        this.featureName = featureName;
    }

    @Column(name = "TEMPLATE")
    public Boolean getTemplate()
    {
        return this.template;
    }

    public void setTemplate(Boolean template)
    {
        this.template = template;
    }

    @Column(name = "DESCRIPTION")
    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    @Column(name = "ICON_NAME")
    public String getIconName()
    {
        if (StringUtils.isNotEmpty(iconName))
        {
            return "default";
        }
        return iconName;
    }

    public void setIconName(String iconName)
    {
        this.iconName = iconName;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "feature", cascade = CascadeType.ALL)
    @OrderBy("id ASC")
    @JsonIgnore
    public Set<Module> getModules()
    {
        return this.modules;
    }

    public void setModules(Set<Module> modules)
    {
        this.modules = modules;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "feature", cascade = CascadeType.ALL)
    @OrderBy("id ASC")
    @JsonIgnore
    public Set<FunctionModule> getFunctionModules()
    {
        return functionModules;
    }

    public void setFunctionModules(Set<FunctionModule> functionModules)
    {
        this.functionModules = functionModules;
    }

    @Column(name = "FUNCTION_NAME")
    public String getFunctionName()
    {
        return functionName;
    }

    public void setFunctionName(String functionName)
    {
        this.functionName = functionName;
    }

    @Transient
    public Boolean getSearched()
    {
        return searched;
    }

    public void setSearched(Boolean searched)
    {
        this.searched = searched;
    }
    
    @Transient
    public List<Module> getTempModules()
    {
        return tempModules;
    }

    public void setTempModules(List<Module> tempModules)
    {
        this.tempModules = tempModules;
    }

    public Object shadowClone() throws CloneNotSupportedException
    {
        Feature cloned = (Feature) super.clone();
        cloned.setId(null);
        cloned.setTemplate(Boolean.FALSE);
        cloned.setFunctionModules(null);
        cloned.setModules(null);

        return cloned;
    }

    public Object clone() throws CloneNotSupportedException
    {
        Feature cloned = (Feature) super.clone();
        cloned.setId(null);
        cloned.setTemplate(Boolean.FALSE);
        if (this.modules != null)
        {
            Set<Module> clonedModules = new HashSet<Module>();
            for (Module module : modules)
            {
                Module clonedModule = (Module) module.clone();
                clonedModule.setFeature(cloned);
                clonedModules.add(clonedModule);
            }

            cloned.setModules(clonedModules);
        }
        
        if(this.functionModules != null)
        {
        	Set<FunctionModule> functionModules = new HashSet<FunctionModule>();
        	for(FunctionModule functionModule : functionModules)
        	{
        		FunctionModule clonedFunctionModule = (FunctionModule)functionModule.clone();
        		clonedFunctionModule.setFeature(cloned);
        		functionModules.add(clonedFunctionModule);
        	}
        	
        	cloned.setFunctionModules(functionModules);
        }

        return cloned;
    }
}
